package xu.ye.view;

import com.agent.crm.db.DBInitScript;
import com.agent.crm.db.DatabaseManager;
import com.agent.crm.view.R;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddEditGroupActivity extends Activity {
    
    private EditText et_groupId;
    private EditText et_groupName;
    private Button btn_save;
    private Button btn_exit;
    private DatabaseManager manager = null;
    private Intent intent;
    private Boolean bool;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        manager = new DatabaseManager(this);
        
        setContentView(R.layout.add_edit_group);
        et_groupId = (EditText)findViewById(R.id.editGroupId);
        et_groupName = (EditText)findViewById(R.id.editGroupNameId);
        btn_save = (Button)findViewById(R.id.editGroupSaveId);
        btn_exit = (Button)findViewById(R.id.editGroupExitId);
        btn_save.setOnClickListener(clickListener);
        btn_exit.setOnClickListener(clickListener);
        intent = getIntent();
        bool = intent.getBooleanExtra("edit", false);
        if(bool){
            et_groupId.setText(intent.getIntExtra("group_id", -1) + "");
            et_groupName.setText(intent.getStringExtra("group_name"));
        }
        
    }
    
    OnClickListener clickListener = new OnClickListener(){
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.editGroupSaveId:
                String groupName = et_groupName.getText().toString();
                if(groupName.trim().equals("")){
                    Toast.makeText(AddEditGroupActivity.this, "组名不许为空", Toast.LENGTH_LONG).show();
                    return;
                }
                SQLiteDatabase SQLite = manager.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("group_name", groupName);
                if(bool){
                    int result = SQLite.update(DBInitScript.Table_GroupInfo, values, "group_id=?", new String[]{et_groupId.getText().toString()});
                    if(result == -1 ) {
                        Toast.makeText(AddEditGroupActivity.this, "更新失败", Toast.LENGTH_LONG);
                    }
                    setTitle("组名修改成功!");
                }
                else{
                    values.put("group_count", 0);
                    long result = SQLite.insert(DBInitScript.Table_GroupInfo, null, values);
                    
                    //通过结果来判断是否插入成功，若为1，则表示插入数据失败
                    if(result == -1 ) {
                        Toast.makeText(AddEditGroupActivity.this, "添加失败", Toast.LENGTH_LONG);
                    }
                    setTitle("分组添加成功!");
                }
                SQLite.close();
                //返回到上一个Activity
                setResult(3); 
                finish();
                break;
                
            case R.id.editGroupExitId:
                finish();
                break;
                
            default:
                break;
            }
            
            
        }
    };
    
    
    
}
